package com.chenfwind.serviceImpl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chenfwind.IDao.FriendInvitationMapper;
import com.chenfwind.IDao.FriendMapper;
import com.chenfwind.common.Constant;
import com.chenfwind.common.StringUtils;
import com.chenfwind.domain.Friend;
import com.chenfwind.domain.FriendInvitation;
import com.chenfwind.service.InvitationService;

@Service("invitationService")
public class InvitationServiceImpl implements InvitationService {

	@Autowired
	private FriendInvitationMapper invitationDao;
	
	@Autowired 
	private FriendMapper friendDao;
	
	public void save(FriendInvitation friendInvitation) {
		// TODO Auto-generated method stub
		invitationDao.insertSelective(friendInvitation);
	}

	public void changeInvitationStatus(FriendInvitation invitation,String status){
		if (Constant.COMMENT_STATUS_SUCCESS.equals(status)) {
			
			invitationDao.updateStatus(invitation.getInviterUserId(),invitation.getInviteeUserId());
			
			Friend friend01 = new Friend();
			friend01.setId(StringUtils.genUUID());
			friend01.setUser1Id(invitation.getInviteeUserId());
			friend01.setUser2Id(invitation.getInviterUserId());
			friend01.setCreateTime(new Date());
			friendDao.insert(friend01);
			Friend friend02 = new Friend();
			friend02.setId(StringUtils.genUUID());
			friend02.setUser2Id(invitation.getInviteeUserId());
			friend02.setUser1Id(invitation.getInviterUserId());
			friend02.setCreateTime(new Date());
			friendDao.insert(friend02);
		} else {
			invitation.setInviteStatus(2);
			invitationDao.updateByPrimaryKeySelective(invitation);
		}
	}
	
	public List<FriendInvitation> findInvitationByUid(String userId){
		return invitationDao.selectInvitationByUid(userId);
	}
	
	public FriendInvitation findInvitationById(String invitationId){
		return invitationDao.selectByPrimaryKey(invitationId);
	}
}
